﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;

namespace IsolatedHelperLibrary
{
    public static class IsolatedStorageHelper
    {
        /// <summary>
        /// Saves an ImageStream to desired location in IsolatedStorage for a given quality factor
        /// </summary>
        /// <param name="imageStream"></param>
        /// <param name="isoPath"></param>
        /// <param name="quality"></param>
        public static void SaveImageStream(System.IO.Stream imageStream, string isoPath, int quality)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                BitmapImage bmp = new BitmapImage();
                bmp.CreateOptions = BitmapCreateOptions.DelayCreation; // added because of WP7.1
                bmp.SetSource(imageStream);
                using (IsolatedStorageFileStream isoStream = store.OpenFile(isoPath, System.IO.FileMode.OpenOrCreate))
                {
                    if (bmp != null && isoStream != null)
                    {
                        WriteableBitmap wbmp = new WriteableBitmap(bmp);
                        Extensions.SaveJpeg(wbmp, isoStream, bmp.PixelWidth, bmp.PixelHeight, 0, quality);
                    }
                    isoStream.Close();
                }
            }
        }

        /// <summary>
        /// Saves a BitmapImage to the path provided
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="isoPath"></param>
        /// <param name="quality"></param>
        public static void SaveBitmap(BitmapImage bmp, string isoPath, int quality)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isoStream = store.OpenFile(isoPath, System.IO.FileMode.OpenOrCreate))
                {
                    if (bmp != null && isoStream != null)
                    {
                        WriteableBitmap wbmp = new WriteableBitmap(bmp);
                        Extensions.SaveJpeg(wbmp, isoStream, bmp.PixelWidth, bmp.PixelHeight, 0, quality);
                    }
                    isoStream.Close();
                }
            }
        }

        /// <summary>
        /// Reads images from an IsolatedStorage location and return it as BitmapImage
        /// </summary>
        /// <param name="isoPath"></param>
        /// <returns>BitmapImage</returns>
        public static BitmapImage GetBitmapImage(string isoPath)
        {
            BitmapImage bmp = null;
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isoStream = store.OpenFile(isoPath, System.IO.FileMode.OpenOrCreate))
                {
                    if (isoStream != null)
                    {
                        bmp = new BitmapImage();
                        bmp.CreateOptions = BitmapCreateOptions.DelayCreation;
                        bmp.SetSource(isoStream);
                    }
                    isoStream.Close();
                }
            }
            return bmp;
        }

        /// <summary>
        /// Creates a folder at given path
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDirectory(string path)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    if (!store.DirectoryExists(path))
                        store.CreateDirectory(path);
                }
                catch { };
            }
        }


        /// <summary>
        /// Returns folder names underneath given location.
        /// If path is not correct reutrns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] GetDirectories(string path)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    return store.GetDirectoryNames(path);
                }
                catch { return null; }
            }
        }

        /// <summary>
        /// Returns all folder underneath root.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] GetDirectories()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    return store.GetDirectoryNames();
                }
                catch { return null; }
            }
        }

        /// <summary>
        /// Returns file names underneath given location and with proper serach criteria.
        /// If name or search criteria is not correct reutrns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] GetFileNames(string path)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    return store.GetFileNames(path);
                }
                catch { return null; }
            }
        }

        /// <summary>
        /// Returns all files names under root.
        /// If 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string[] GetFileNames()
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try { return store.GetFileNames(); }
                catch { return null; }
            }
        }
    }
}
